home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 June: Reference Library / Dev.CD Jun 95 / Dev.CD Jun 95.toast / What's New? / New System Software Extensions / QuickDraw 3D ß / Programming / SampleCode / 3DMF2PICT / 3DMF2PICTSupport.c < prev    next >
Encoding:
Text File  |  1995-04-15  |  17.7 KB  |  652 lines  |  [TEXT/MPCC]

  1. // My3dSupport.c - QuickDraw 3d routines
  2. //
  3. // This file illustrates how to set up a pixmap based draw context.
  4. // A metafile is read into and imaged in the pixmap, when the window is
  5. // closed the pixmap is used to generate a PICT of the scene.
  6. //
  7. // Created 27th Dec 1994, Nick Thompson, DEVSUPPORT
  8. //
  9. // © 1995, Apple Computer Inc., All Rights Reserved
  10.  
  11.  
  12.  
  13. #include <Files.h>
  14. #include <QuickDraw.h>
  15. #include <QDOffScreen.h>
  16. #include <StandardFile.h>
  17.  
  18. #include "3DMF2PICTSupport.h"
  19.  
  20. #include "QD3D.h"
  21. #include "QD3DDrawContext.h"
  22. #include "QD3DRenderer.h"
  23. #include "QD3DShader.h"
  24. #include "QD3DCamera.h"
  25. #include "QD3DLight.h"
  26. #include "QD3DGeometry.h"
  27. #include "QD3DGroup.h"
  28. #include "QD3DMath.h"
  29. #include "QD3DTransform.h"
  30. #include "QD3DStorage.h"
  31. #include "QD3DIO.h"
  32.  
  33.  
  34. //-----------------------------------------------------------------------------------------------
  35. // local utility functions
  36. static    TQ3FileObject         MyGetNewFile( FSSpec *myFSSpec, TQ3Boolean *isText ) ;
  37.  
  38. static void GetGroupBBox(
  39.     DocumentPtr            theDocument,
  40.     TQ3BoundingBox         *viewBBox) ;
  41.                                                 
  42. static    TQ3Status MyAddShaderToGroup( TQ3GroupObject group ) ;
  43.  
  44. static TQ3Status GetDocumentGroupBoundingBox( 
  45.     DocumentPtr theDocument , 
  46.     TQ3BoundingBox *viewBBox) ;
  47.  
  48. //-----------------------------------------------------------------------------------------------
  49. // Submit the scene for rendering/fileIO and picking
  50. TQ3Status SubmitScene( DocumentPtr theDocument ) 
  51. {        
  52.     TQ3Vector3D                globalScale;
  53.     TQ3Vector3D                globalTranslate;
  54.     
  55.     globalScale.x = globalScale.y = globalScale.z = theDocument->fGroupScale;
  56.     globalTranslate = *(TQ3Vector3D *)&theDocument->fGroupCenter;
  57.     Q3Vector3D_Scale(&globalTranslate, -1, &globalTranslate);
  58.     Q3Style_Submit(theDocument->fInterpolation, theDocument->fView);
  59.     Q3Style_Submit(theDocument->fBackFacing , theDocument->fView);
  60.     Q3Style_Submit(theDocument->fFillStyle, theDocument->fView);
  61.         
  62.     Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView);
  63.         
  64.     Q3ScaleTransform_Submit(&globalScale, theDocument->fView);
  65.     Q3TranslateTransform_Submit(&globalTranslate, theDocument->fView);
  66.     Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView);
  67.     
  68.     return kQ3Success ;
  69. }
  70.  
  71. //-----------------------------------------------------------------------------------------------
  72.  
  73. static TQ3Status GetDocumentGroupBoundingBox( 
  74.     DocumentPtr theDocument , 
  75.     TQ3BoundingBox *viewBBox)
  76. {
  77.     TQ3Status        status;
  78.     TQ3ViewStatus    viewStatus ;
  79.     
  80.     status = Q3View_StartBoundingBox( theDocument->fView, kQ3ComputeBoundsApproximate );
  81.     do {
  82.         status = SubmitScene( theDocument ) ;
  83.     } while((viewStatus = Q3View_EndBoundingBox( theDocument->fView, viewBBox )) == kQ3ViewStatusRetraverse );
  84.     return status ;
  85. }
  86. //-----------------------------------------------------------------------------------------------
  87.  
  88. TQ3ViewObject MyNewView(GWorldPtr myOffscreenGWorld)
  89. {
  90.     TQ3Status                myStatus;
  91.     TQ3ViewObject            myView;
  92.     TQ3DrawContextObject        myDrawContext;
  93.     TQ3RendererObject        myRenderer;
  94.     TQ3CameraObject            myCamera;
  95.     TQ3GroupObject            myLights;
  96.     
  97.     if((myView = Q3View_New()) == NULL)
  98.         goto bail ;    
  99.         
  100.     //    Create and set draw context.
  101.     if ((myDrawContext = MyNewDrawContext(myOffscreenGWorld)) == NULL )
  102.         goto bail;
  103.         
  104.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
  105.         goto bail;
  106.  
  107.     Q3Object_Dispose( myDrawContext ) ;
  108.     
  109.     //    Create and set renderer.
  110.     // this would use the interactive software renderer
  111.  
  112.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != NULL ) {
  113.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  114.             goto bail;
  115.         }
  116.     }
  117.     else {
  118.         goto bail;
  119.     }
  120.  
  121.     Q3Object_Dispose( myRenderer ) ;
  122.     
  123.     //    Create and set camera.
  124.     if ( (myCamera = MyNewCamera(myOffscreenGWorld)) == NULL )
  125.         goto bail;
  126.         
  127.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
  128.         goto bail;
  129.  
  130.     Q3Object_Dispose( myCamera ) ;
  131.     
  132.     //    Create and set lights.
  133.     if ((myLights = MyNewLights()) == NULL )
  134.         goto bail;
  135.         
  136.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
  137.         goto bail;
  138.         
  139.     Q3Object_Dispose(myLights);
  140.  
  141.     //    Done!!!
  142.     return ( myView );
  143.     
  144. bail:
  145.     //    If any of the above failed, then don't return a view.
  146.     SysBeep(10) ;
  147.     return ( NULL );
  148. }
  149.  
  150. //----------------------------------------------------------------------------------
  151.  
  152. TQ3DrawContextObject MyNewDrawContext(GWorldPtr theGWorld)
  153. {
  154.     TQ3PixmapDrawContextData    myDrawContextData;
  155.     TQ3ColorARGB                clearColor ;
  156.     PixMapHandle                 hPixMap ;
  157.     Rect                        srcRect ;
  158.     
  159.     //    Set the background color.
  160.     clearColor.a = 1.0;
  161.     clearColor.r = 1.0;
  162.     clearColor.g = 1.0;
  163.     clearColor.b = 1.0;
  164.     
  165.     //    Fill in draw context data.
  166.             
  167.     myDrawContextData.drawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  168.     myDrawContextData.drawContextData.clearImageColor  = clearColor;
  169.  
  170.     myDrawContextData.drawContextData.paneState = kQ3False;
  171.     myDrawContextData.drawContextData.maskState = kQ3False;
  172.     
  173.     myDrawContextData.drawContextData.doubleBufferState = kQ3False;
  174.  
  175.     hPixMap = GetGWorldPixMap(theGWorld);
  176.     LockPixels(hPixMap);
  177.  
  178.     srcRect = theGWorld->portRect;
  179.  
  180.     myDrawContextData.pixmap.width = srcRect.right  - srcRect.left;
  181.     myDrawContextData.pixmap.height= srcRect.bottom - srcRect.top;
  182.     
  183.     myDrawContextData.pixmap.rowBytes = (**hPixMap).rowBytes & 0x7FFF;
  184.     myDrawContextData.pixmap.pixelType = kQ3PixelTypeRGB32;
  185.     myDrawContextData.pixmap.pixelSize = 32;
  186.  
  187.     myDrawContextData.pixmap.bitOrder  = kQ3EndianBig;
  188.     myDrawContextData.pixmap.byteOrder = kQ3EndianBig;
  189.     
  190.     myDrawContextData.pixmap.image        = GetPixBaseAddr( hPixMap );
  191.     
  192.     return Q3PixmapDrawContext_New(&myDrawContextData);
  193. }
  194. //----------------------------------------------------------------------------------
  195.  
  196. TQ3CameraObject MyNewCamera(CGrafPtr thePort)
  197. {
  198.     TQ3CameraObject                    myCamera;
  199.     TQ3CameraData                    myCameraData;
  200.     TQ3ViewAngleAspectCameraData        myViewAngleCameraData;
  201.     TQ3Point3D                        cameraFrom     = { 0.0, 0.0, 30.0 };
  202.     TQ3Point3D                        cameraTo     = { 0.0, 0.0, 0.0 };
  203.     TQ3Vector3D                        cameraUp     = { 0.0, 1.0, 0.0 };
  204.     
  205.     float                             fieldOfView = .52359333333;
  206.     float                             hither         = 0.001;
  207.     float                             yon         = 1000;
  208.     
  209.     //    Fill in camera data.
  210.     myCameraData.placement.cameraLocation = cameraFrom;
  211.     myCameraData.placement.pointOfInterest = cameraTo;
  212.     myCameraData.placement.upVector = cameraUp;
  213.     
  214.     myCameraData.range.hither = hither;
  215.     myCameraData.range.yon = yon;
  216.     
  217.     myCameraData.viewPort.origin.x = -1.0;
  218.     myCameraData.viewPort.origin.y = 1.0;
  219.     myCameraData.viewPort.width = 2.0;
  220.     myCameraData.viewPort.height = 2.0;
  221.     
  222.     myViewAngleCameraData.cameraData = myCameraData;
  223.     myViewAngleCameraData.fov = fieldOfView ;
  224.     
  225.     // set up the aspect ratio based on the window
  226.     myViewAngleCameraData.aspectRatioXToY =  
  227.             (float) (thePort->portRect.right - thePort->portRect.left) / 
  228.             (float) (thePort->portRect.bottom - thePort->portRect.top);
  229.  
  230.     myCamera = Q3ViewAngleAspectCamera_New(&myViewAngleCameraData);    
  231.     
  232.     //    Return the camera.
  233.     return ( myCamera );
  234. }
  235.  
  236.  
  237. //----------------------------------------------------------------------------------
  238.  
  239. TQ3GroupObject MyNewLights()
  240. {
  241.     TQ3GroupPosition            myGroupPosition;
  242.     TQ3GroupObject            myLightList;
  243.     TQ3LightData                myLightData;
  244.     TQ3PointLightData        myPointLightData;
  245.     TQ3DirectionalLightData    myDirectionalLightData;
  246.     TQ3LightObject            myAmbientLight, myPointLight, myFillLight;
  247.     TQ3Point3D                pointLocation = { -10.0, 0.0, 10.0 };
  248.     TQ3Vector3D                fillDirection = { 10.0, 0.0, 10.0 };
  249.     TQ3ColorRGB                WhiteLight = { 1.0, 1.0, 1.0 };
  250.     
  251.     //    Set up light data for ambient light.  This light data will be used for point and fill
  252.     //    light also.
  253.  
  254.     myLightData.isOn = kQ3True;
  255.     myLightData.color = WhiteLight;
  256.     
  257.     //    Create ambient light.
  258.     myLightData.brightness = .2;
  259.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  260.     if ( myAmbientLight == NULL )
  261.         goto bail;
  262.     
  263.     //    Create point light.
  264.     myLightData.brightness = 1.0;
  265.     myPointLightData.lightData = myLightData;
  266.     myPointLightData.castsShadows = kQ3False;
  267.     myPointLightData.attenuation = kQ3AttenuationTypeNone;
  268.     myPointLightData.location = pointLocation;
  269.     myPointLight = Q3PointLight_New(&myPointLightData);
  270.     if ( myPointLight == NULL )
  271.         goto bail;
  272.  
  273.     //    Create fill light.
  274.     myLightData.brightness = .2;
  275.     myDirectionalLightData.lightData = myLightData;
  276.     myDirectionalLightData.castsShadows = kQ3False;
  277.     myDirectionalLightData.direction = fillDirection;
  278.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  279.     if ( myFillLight == NULL )
  280.         goto bail;
  281.  
  282.     //    Create light group and add each of the lights into the group.
  283.     myLightList = Q3LightGroup_New();
  284.     if ( myLightList == NULL )
  285.         goto bail;
  286.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  287.     if ( myGroupPosition == 0 )
  288.         goto bail;
  289.     myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
  290.     if ( myGroupPosition == 0 )
  291.         goto bail;
  292.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  293.     if ( myGroupPosition == 0 )
  294.         goto bail;
  295.  
  296.     Q3Object_Dispose( myAmbientLight ) ;
  297.     Q3Object_Dispose( myPointLight ) ;
  298.     Q3Object_Dispose( myFillLight ) ;
  299.  
  300.     //    Done!
  301.     return ( myLightList );
  302.     
  303. bail:
  304.     //    If any of the above failed, then return nothing!
  305.     return ( NULL );
  306. }
  307.  
  308. //----------------------------------------------------------------------------------
  309.  
  310. TQ3GroupObject MyNewModelFromFile(FSSpec *theFileSpec)
  311. {
  312.     TQ3GroupObject        myGroup = NULL ;
  313.     TQ3Boolean            isText = kQ3False ;
  314.     TQ3FileMode            myFileMode ;    // we are reading the file
  315.     TQ3FileObject        theFile = kQ3FileModeNormal;
  316.     
  317.     //    Create a ordered group for the complete model.
  318.     if ((myGroup = Q3OrderedDisplayGroup_New()) == NULL )
  319.         return NULL;
  320.  
  321.     theFile = MyGetNewFile( theFileSpec, &isText ) ;
  322.     
  323.     if( isText == kQ3True )
  324.         myFileMode |= kQ3FileModeText;    // is it a text metafile??    
  325.  
  326.     // Open the file object
  327.     if( Q3File_OpenRead( theFile, &myFileMode ) != kQ3Success)
  328.         return  NULL ;
  329.  
  330.     MyReadModelFromFile( theFile, myGroup ) ;
  331.     
  332.     Q3File_Close(theFile);            // close and dispose of the file object
  333.     Q3Object_Dispose(theFile);
  334.     
  335.     MyAddShaderToGroup( myGroup ) ;
  336.     
  337.     return myGroup ;
  338. }
  339.  
  340.  
  341. //----------------------------------------------------------------------------------
  342. // attach a shader to the group
  343.  
  344. TQ3Status MyAddShaderToGroup( TQ3GroupObject group )
  345. {
  346.     TQ3ShaderObject    illuminationShader = Q3PhongIllumination_New();
  347.  
  348.     Q3Group_AddObject(group, illuminationShader);
  349.     Q3Object_Dispose(illuminationShader);
  350.     return(kQ3Success);
  351. }
  352.  
  353. //----------------------------------------------------------------------------------
  354. // read model from file object into the supplied group
  355.  
  356. TQ3Status MyReadModelFromFile( TQ3FileObject theFile,TQ3GroupObject myGroup)
  357. {    
  358.     if(theFile != NULL) {
  359.     
  360.         TQ3Object            myTempObj ;
  361.         TQ3Boolean            isEOF ;
  362.                 
  363.     
  364.         // read objects from the file
  365.         do {
  366.         
  367.             Q3File_ReadObject( theFile, &myTempObj );
  368.             
  369.             if( myTempObj != NULL ) {
  370.                 // we only want the object in our main group if we can draw it
  371.                 if (Q3Object_IsDrawable( myTempObj) ) 
  372.                     Q3Group_AddObject( myGroup, myTempObj ) ;
  373.                 
  374.                 // we either added the object to the main group, or we don't care
  375.                 // so we can safely dispose of the object
  376.                 Q3Object_Dispose( myTempObj ) ;
  377.             }
  378.             
  379.             // check to see if we reached the end of file yet
  380.             Q3File_IsEndOfFile( theFile, &isEOF );
  381.             
  382.         } while (isEOF == kQ3False);    
  383.     }
  384.     
  385.     if( myGroup != NULL )
  386.         return kQ3Success ;
  387.     else
  388.         return kQ3Failure ;
  389. }
  390.  
  391. //-----------------------------------------------------------------------------------------------
  392. // cleaned up from IM QuickDraw 3D pp 15-5
  393. static TQ3FileObject MyGetNewFile( FSSpec *myFSSpec, TQ3Boolean *isText )
  394. {
  395.     TQ3FileObject        myFileObj;
  396.     TQ3StorageObject        myStorageObj;
  397.     OSType                myFileType;
  398.     
  399.     FInfo                fndrInfo ;
  400.  
  401.     // we assume the FSSpec passed in was valid, get the file information
  402.     // we need to know the file type, this routine may get called by an appleEvent
  403.     // handler, so we can't assume a type, we need to get it from the fsspec.
  404.     
  405.     FSpGetFInfo( myFSSpec, &fndrInfo ) ;
  406.     
  407.     // pull out the file type
  408.     
  409.     myFileType = fndrInfo.fdType ;
  410.     
  411.     // Create new storage object and new file object 
  412.     if(((myStorageObj = Q3FSSpecStorage_New( myFSSpec )) == NULL) 
  413.         || ((myFileObj = Q3File_New()) == NULL)) 
  414.     {
  415.         if (myStorageObj != NULL) 
  416.             Q3Object_Dispose(myStorageObj);
  417.         return(NULL);
  418.     }
  419.  
  420.     // Set the storage for the file object
  421.     Q3File_SetStorage(myFileObj, myStorageObj);
  422.     Q3Object_Dispose(myStorageObj);
  423.  
  424.     if ((myFileType == '3DMF') || (myFileType == 'EO3D'))
  425.         *isText = kQ3False ;
  426.     else if (myFileType == 'TEXT')
  427.         *isText = kQ3True ;
  428.  
  429.     return (myFileObj);
  430. }
  431.  
  432.  
  433. //-------------------------------------------------------------------------------------------
  434. //
  435. Boolean MetafileFileSpecify( FSSpec *theFile )
  436. {
  437.     StandardFileReply    theSFReply ;
  438.     SFTypeList            myTypes = { 'TEXT', '3DMF' } ;
  439.     const short            numTypes = 2 ;
  440.         
  441.     // Get the file name to open
  442.     StandardGetFile( nil, numTypes, myTypes, &theSFReply ) ;
  443.     
  444.     if( theSFReply.sfGood )
  445.         *theFile = theSFReply.sfFile ;
  446.     
  447.     // did the user cancel?
  448.     return theSFReply.sfGood ;
  449.     
  450. }
  451. //----------------------------------------------------------------------------------
  452.  
  453.  
  454. void GetGroupBBox(
  455.     DocumentPtr            theDocument,
  456.     TQ3BoundingBox         *viewBBox)
  457. {
  458.     TQ3Point3D                     from     = { 0.0, 0.0, 1.0 };
  459.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  460.     TQ3Vector3D                     up         = { 0.0, 1.0, 0.0 };
  461.     
  462.     float                         fieldOfView = .52359333333;
  463.     float                         hither         =  0.5;
  464.     float                         yon         =  1.5;
  465.     TQ3GroupObject                mainGroup = theDocument->fModel ;
  466.  
  467.     TQ3Status                    status;
  468.     
  469. #ifdef BETA_1_BUILD
  470.     Q3View_StartBounds( theDocument->fView );
  471.  
  472.     status = Q3DisplayGroup_BoundingBox(mainGroup, 
  473.                                         viewBBox, 
  474.                                         kQ3ComputeBoundsApproximate,
  475.                                          viewObject);
  476.  
  477.     Q3View_EndBounds( theDocument->fView );
  478. #else
  479.     status = GetDocumentGroupBoundingBox( theDocument , viewBBox) ;
  480. #endif
  481.                                         
  482.     //
  483.     //  If we have a point model, then the "viewBBox" would end up
  484.     //  being a "singularity" at the location of the point.  As
  485.     //  this bounding "box" is used in setting up the camera spec,
  486.     //  we get bogus input into Escher.
  487.     
  488.     {
  489.          float        xSize, ySize, zSize;
  490.         
  491.         xSize = viewBBox->max.x - viewBBox->min.x;
  492.         ySize = viewBBox->max.y - viewBBox->min.y;
  493.         zSize = viewBBox->max.z - viewBBox->min.z;
  494.  
  495.         if (xSize <= kQ3RealZero &&
  496.             ySize <= kQ3RealZero &&
  497.             zSize <= kQ3RealZero) {
  498.             
  499.             viewBBox->max.x += 0.0001;
  500.             viewBBox->max.y += 0.0001;
  501.             viewBBox->max.z += 0.0001;
  502.             
  503.             viewBBox->min.x -= 0.0001;
  504.             viewBBox->min.y -= 0.0001;
  505.             viewBBox->min.z -= 0.0001;
  506.         }
  507.     }
  508. }
  509.  
  510.  
  511.  
  512.  
  513. //------------------------------------------------------------------------
  514.  
  515.  
  516. TQ3Point3D AdjustCamera(
  517.     DocumentPtr            theDocument,
  518.     short                winWidth,
  519.     short                winHeight)
  520. {
  521.     float                         fieldOfView;
  522.     float                         hither;
  523.     float                         yon;
  524.     TQ3CameraPlacement            placement;
  525.     TQ3CameraRange                range;
  526.     TQ3BoundingBox                 viewBBox;
  527.     long                         fromAxis;    
  528.     float                         maxDimension;
  529.      float                        xSize, ySize, zSize;
  530.     float                        weights[2] = { 0.5, 0.5 };
  531.     TQ3Point3D                    points[2];
  532.     TQ3Vector3D                     viewVector;
  533.     TQ3Vector3D                    normViewVector;
  534.     TQ3Vector3D                    eyeToFrontClip;
  535.     TQ3Vector3D                    eyeToBackClip;
  536.     float                        viewDistance;
  537.     TQ3Vector3D                    diagonalVector;
  538.     float                        ratio;
  539.     TQ3CameraObject                camera;
  540.     
  541.     TQ3ViewObject                theView = theDocument->fView ;
  542.     TQ3GroupObject                mainGroup = theDocument->fModel ;
  543.     
  544.     TQ3Point3D                    *documentGroupCenter = &theDocument->fGroupCenter ;
  545.     float                        *documentGroupScale  = &theDocument->fGroupScale ;
  546.  
  547.     Q3View_GetCamera( theView, &camera);
  548.     GetGroupBBox( theDocument, &viewBBox);
  549.  
  550.     /*
  551.      *  If we have a point model, then the "viewBBox" would end up
  552.      *  being a "singularity" at the location of the point.  As
  553.      *  this bounding "box" is used in setting up the camera spec,
  554.      *  we get bogus input into Escher.
  555.      */
  556.     xSize = viewBBox.max.x - viewBBox.min.x;
  557.     ySize = viewBBox.max.y - viewBBox.min.y;
  558.     zSize = viewBBox.max.z - viewBBox.min.z;
  559.  
  560.     if (xSize <= kQ3RealZero &&
  561.         ySize <= kQ3RealZero &&
  562.         zSize <= kQ3RealZero)  {
  563.         viewBBox.max.x += 0.0001;
  564.         viewBBox.max.y += 0.0001;
  565.         viewBBox.max.z += 0.0001;
  566.         
  567.         viewBBox.min.x -= 0.0001;
  568.         viewBBox.min.y -= 0.0001;
  569.         viewBBox.min.z -= 0.0001;
  570.     }
  571.  
  572.     points[0] = viewBBox.min;
  573.     points[1] = viewBBox.max;
  574.  
  575.     Q3Point3D_AffineComb(points, weights, 2, documentGroupCenter);
  576.  
  577.     /*
  578.      *  The "from" point is on a vector perpendicular to the plane
  579.      *  in which the bounding box has greatest dimension.  As "up" is
  580.      *  always in the positive y direction, look at x and z directions.
  581.      */
  582.     xSize = viewBBox.max.x - viewBBox.min.x;
  583.     zSize = viewBBox.max.z - viewBBox.min.z;
  584.     
  585.     if (xSize > zSize) {
  586.         fromAxis = kQ3AxisZ;
  587.     } else {
  588.         fromAxis = kQ3AxisX;
  589.     }
  590.  
  591.     /*
  592.      *  Compute the length of the diagonal of the bounding box.
  593.      *
  594.      *  The hither and yon planes are adjusted so that the
  595.       *  diagonal of the bounding box is 7/8 the size of the
  596.       *  minimum dimension of the view frustum. The diagonal is used instead
  597.       *  of the maximum size (in x, y, or z) so that when you rotate
  598.       *  the object, the corners don't get clipped out.
  599.       */
  600.     Q3Point3D_Subtract(
  601.         &viewBBox.max,
  602.         &viewBBox.min,
  603.         &diagonalVector);
  604.  
  605.     maxDimension    =    Q3Vector3D_Length(&diagonalVector);
  606.     maxDimension    *=    8.0 / 7.0;
  607.     
  608.     ratio = 1.0 / maxDimension;
  609.             
  610.     *documentGroupScale = ratio;
  611.     
  612.     Q3Camera_GetPlacement(camera, &placement);
  613.  
  614.     Q3Point3D_Subtract(
  615.         &placement.cameraLocation,
  616.         &placement.pointOfInterest,
  617.         &viewVector);
  618.         
  619.     viewDistance = Q3Vector3D_Length(&viewVector);
  620.     
  621.     Q3Vector3D_Normalize(&viewVector, &normViewVector);
  622.     
  623.     Q3Vector3D_Scale(&normViewVector, 
  624.                      viewDistance - ratio * maxDimension/2.0,
  625.                      &eyeToFrontClip);
  626.                     
  627.     Q3Vector3D_Scale(&normViewVector, 
  628.                     viewDistance + ratio * maxDimension/2.0,
  629.                     &eyeToBackClip);
  630.  
  631.     hither     = Q3Vector3D_Length(&eyeToFrontClip);
  632.     yon     = Q3Vector3D_Length(&eyeToBackClip);
  633.     
  634.     fieldOfView = 2 * atan((ratio * maxDimension/2.0)/hither);
  635.  
  636.     range.hither                 = hither;
  637.     range.yon                     = yon;
  638.  
  639.     Q3Camera_SetRange(camera, &range);
  640.  
  641.     Q3ViewAngleAspectCamera_SetFOV(
  642.         camera, fieldOfView);
  643.  
  644.     Q3ViewAngleAspectCamera_SetAspectRatio(
  645.         camera, (float) winWidth / (float) winHeight);
  646.  
  647.     Q3Object_Dispose(camera);
  648.     
  649.     return( *documentGroupCenter );
  650. }
  651.  
  652.